package com.rc.saas.tenant.service.market;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rc.saas.tenant.mapper.market.MarketActivityRecordMapper;
import com.rc.saas.tenant.model.market.MarketActivity;
import com.rc.saas.tenant.model.market.MarketActivityRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2021/9/16
 */
@Service
public class MarketActivityRecordServiceImpl implements MarketActivityRecordService {

    @Autowired
    private MarketActivityRecordMapper marketActivityRecordMapper;

    @Override
    public MarketActivityRecordMapper getMapper() {
        return marketActivityRecordMapper;
    }

    @Override
    @Transactional
    public int saveActivityChild(MarketActivity marketActivity) {
        if (StringUtils.isBlank(marketActivity.getActivityType())){
            return 0;
        }
        List<MarketActivityRecord> childLst = null;
        switch (marketActivity.getActivityType()){
            case "1" ://充值活动
                childLst = new ArrayList<>();
                JSONArray chargeArray = JSONArray.parseArray(marketActivity.getChargeLstStr());
                for (int i = 0; i <chargeArray.size() ; i++) {
                    JSONObject temp = chargeArray.getJSONObject(i);
                    MarketActivityRecord childInfo = new MarketActivityRecord();
                    childInfo.setMarketActivityId(marketActivity.getMarketActivityId());
                    childInfo.setCreatedTime(new Date());
                    childInfo.setCreatedBy(marketActivity.getCreatedBy());
                    childInfo.setGearType("3");
                    String radioStr = temp.getString("chargeRadio_cz");
                    childInfo.setRewardType(radioStr);
                    if (StringUtils.equals("0",radioStr)){//奖励类型 0奖励金额 1奖励卡券(新增)
                        childInfo.setChargeAmount(temp.getInteger("chargeAmount_cz"));
                        childInfo.setAwardAmount(temp.getInteger("awardAmount_cz"));
                    }else{
                        childInfo.setChargeAmount(temp.getInteger("chargeAmount_cz"));
                        childInfo.setTicketTypeId(temp.getLong("tickettypeId_cz"));
                    }
                    childLst.add(childInfo);
                }
                //marketActivityRecordMapper.batchInsert(childLst);
                break;
            case "3" ://满减活动
                childLst = new ArrayList<>();
                JSONArray redutionArray = JSONArray.parseArray(marketActivity.getRedutionLstStr());
                for (int i = 0; i <redutionArray.size() ; i++) {
                    JSONObject temp = redutionArray.getJSONObject(i);
                    MarketActivityRecord childInfo = new MarketActivityRecord();
                    childInfo.setMarketActivityId(marketActivity.getMarketActivityId());
                    childInfo.setCreatedTime(new Date());
                    childInfo.setCreatedBy(marketActivity.getCreatedBy());
                    childInfo.setGearType("0");
                    childInfo.setRewardType("0");
                    childInfo.setOrderAmount(temp.getInteger("orderAmount_mj"));
                    childInfo.setAwardAmount(temp.getInteger("awardAmount_mj"));
                    childLst.add(childInfo);
                }
                //marketActivityRecordMapper.batchInsert(childLst);
                break;
            case "4" ://满返活动
                childLst = new ArrayList<>();
                JSONArray returnArray = JSONArray.parseArray(marketActivity.getReturnLstStr());
                for (int i = 0; i <returnArray.size() ; i++) {
                    JSONObject temp = returnArray.getJSONObject(i);
                    MarketActivityRecord childInfo = new MarketActivityRecord();
                    childInfo.setMarketActivityId(marketActivity.getMarketActivityId());
                    childInfo.setCreatedTime(new Date());
                    childInfo.setCreatedBy(marketActivity.getCreatedBy());
                    childInfo.setGearType("1");
                    childInfo.setRewardType("1");
                    childInfo.setOrderAmount(temp.getInteger("orderAmount_mf"));
                    childInfo.setTicketTypeId(temp.getLong("tickettypeId_mf"));
                    childLst.add(childInfo);
                }
               // marketActivityRecordMapper.batchInsert(childLst);
                break;
            case "5" ://拉新活动
                childLst = new ArrayList<>();
                JSONArray newArray = JSONArray.parseArray(marketActivity.getNewLstStr());
                for (int i = 0; i <newArray.size() ; i++) {
                    JSONObject temp = newArray.getJSONObject(i);
                    MarketActivityRecord childInfo = new MarketActivityRecord();
                    childInfo.setMarketActivityId(marketActivity.getMarketActivityId());
                    childInfo.setCreatedTime(new Date());
                    childInfo.setCreatedBy(marketActivity.getCreatedBy());
                    childInfo.setGearType("2");
                    String radioStr = temp.getString("pullNewRadio_lx");
                    childInfo.setRewardType(radioStr);
                    if (StringUtils.equals("0",radioStr)){//奖励类型 0奖励金额 1奖励卡券(新增)
                        childInfo.setPullNew(temp.getInteger("pullNew_lx"));
                        childInfo.setAwardAmount(temp.getInteger("awardAmount_lx"));
                    }else{
                        childInfo.setPullNew(temp.getInteger("pullNew_lx"));
                        childInfo.setTicketTypeId(temp.getLong("ticketTypeId_lx"));
                    }
                    childLst.add(childInfo);
                }
                break;
        }
        if (childLst!=null){
            marketActivityRecordMapper.batchInsert(childLst);
        }
        return 1;
    }
}
